package com.airlines.runlimit.controller;


import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.runlimit.domain.BasTeaching;
import com.airlines.runlimit.service.BasTeachingService;

/**
 * 教员带飞
 * @author caoyang
 * @DATE 2018/9/14
 */

@Controller
@RequestMapping("/basTeaching")
public class BasTeachingController {

    private static Logger logger = LoggerFactory.getLogger(BasTeaching.class);

    private static final String PREFIX = "/runlimit";

    @Autowired
    private BasTeachingService basTeachingService;

    /**
     * 进入页面
     *
     * @return
     */
    @RequestMapping(value = "", method = {RequestMethod.GET})
    String toBasTeachingPage() {
        logger.info("Cuurrent Time: {}",System.currentTimeMillis());
        return PREFIX + "/basTeachingList";
    }

    /**
     * 多条件模糊 查询并且分页显示
     *
     * @param basTeaching
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Page list(BasTeaching basTeaching, String offset, String limit) {
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        basTeaching.setIsDelete(1);
        page = basTeachingService.listData(basTeaching, page);
        return page;
    }


    /**
     * 进入新增页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.GET})
    String add() {
        return PREFIX + "/basTeachingAdd";
    }

    /**
     * 保存
     *
     * @param basTeaching
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public R save(BasTeaching basTeaching, HttpServletRequest request) {
        basTeaching.setTeachId(UUID.randomUUID().toString());
        basTeaching.setIsDelete(1);
        if (basTeachingService.saveSelective(basTeaching) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    String edit(Model model, String id) {
        BasTeaching basTeaching = basTeachingService.getByPrimaryKey(id);
        model.addAttribute("basTeaching", basTeaching);
        return PREFIX + "/basTeachingEdit";
    }

    /**
     * 修改
     *
     * @param basTeaching
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public R update(BasTeaching basTeaching, HttpServletRequest request) {
        if (basTeachingService.updateByPrimaryKeySelective(basTeaching) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public R delete(String id) {
        BasTeaching basTeaching = basTeachingService.getByPrimaryKey(id);
        basTeaching.setIsDelete(0);
        if (basTeachingService.updateByPrimaryKeySelective(basTeaching) > 0) {
            return R.ok();
        }
        return R.error();
    }

}
